home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Microsoft Visual Basic .NET
/
Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin
/
22 xml and adonet
/
xmldemo
/
xmldatadocumentform.vb
< prev
next >
Wrap
Text File
|
2002-03-16
|
8KB
|
205 lines
Imports System.Data
Imports System.Data.OleDb
Imports System.Xml
Public Class XmlDataDocumentForm
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnFill As System.Windows.Forms.Button
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
Friend WithEvents btnRefresh As System.Windows.Forms.Button
Friend WithEvents btnAddNode As System.Windows.Forms.Button
Friend WithEvents bthXPathSearches As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnFill = New System.Windows.Forms.Button()
Me.DataGrid1 = New System.Windows.Forms.DataGrid()
Me.TreeView1 = New System.Windows.Forms.TreeView()
Me.btnRefresh = New System.Windows.Forms.Button()
Me.btnAddNode = New System.Windows.Forms.Button()
Me.bthXPathSearches = New System.Windows.Forms.Button()
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'btnFill
'
Me.btnFill.Location = New System.Drawing.Point(8, 16)
Me.btnFill.Name = "btnFill"
Me.btnFill.Size = New System.Drawing.Size(104, 40)
Me.btnFill.TabIndex = 0
Me.btnFill.Text = "Fill the DataSet"
'
'DataGrid1
'
Me.DataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right)
Me.DataGrid1.CaptionText = "Publishers and Titles"
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(128, 16)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(496, 184)
Me.DataGrid1.TabIndex = 1
'
'TreeView1
'
Me.TreeView1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right)
Me.TreeView1.ImageIndex = -1
Me.TreeView1.Location = New System.Drawing.Point(128, 216)
Me.TreeView1.Name = "TreeView1"
Me.TreeView1.SelectedImageIndex = -1
Me.TreeView1.Size = New System.Drawing.Size(496, 144)
Me.TreeView1.TabIndex = 2
'
'btnRefresh
'
Me.btnRefresh.Location = New System.Drawing.Point(8, 72)
Me.btnRefresh.Name = "btnRefresh"
Me.btnRefresh.Size = New System.Drawing.Size(104, 40)
Me.btnRefresh.TabIndex = 3
Me.btnRefresh.Text = "Refresh the TreeView"
'
'btnAddNode
'
Me.btnAddNode.Location = New System.Drawing.Point(8, 128)
Me.btnAddNode.Name = "btnAddNode"
Me.btnAddNode.Size = New System.Drawing.Size(104, 40)
Me.btnAddNode.TabIndex = 4
Me.btnAddNode.Text = "Add a new Node"
'
'bthXPathSearches
'
Me.bthXPathSearches.Location = New System.Drawing.Point(8, 184)
Me.bthXPathSearches.Name = "bthXPathSearches"
Me.bthXPathSearches.Size = New System.Drawing.Size(104, 40)
Me.bthXPathSearches.TabIndex = 5
Me.bthXPathSearches.Text = "XPath searches"
'
'XmlDataDocumentForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
Me.ClientSize = New System.Drawing.Size(632, 365)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.bthXPathSearches, Me.btnAddNode, Me.btnRefresh, Me.TreeView1, Me.DataGrid1, Me.btnFill})
Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Name = "XmlDataDocumentForm"
Me.Text = "XmlDataDocument Demo"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Dim ds As New DataSet()
Dim xdd As XmlDataDocument
' fill a dataset
Private Sub btnFill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFill.Click
Dim cn As New OleDbConnection(OledbPubsConnString)
Dim daPubs As New OleDbDataAdapter("SELECT * FROM Publishers", cn)
Dim daTitles As New OleDbDataAdapter("SELECT * FROM Titles", cn)
' Fill the DataSet.
cn.Open()
daPubs.Fill(ds, "Publishers")
daTitles.Fill(ds, "Titles")
cn.Close()
' Create a (nested) relation between the two tables.
Dim rel As New DataRelation("PubsTitles", ds.Tables("Publishers").Columns("pub_id"), ds.Tables("Titles").Columns("pub_id"))
ds.Relations.Add(rel)
rel.Nested = True
' Associate the XmlDataDocument with the DataSet
xdd = New XmlDataDocument(ds)
' Bind the DataGrid
DataGrid1.DataSource = ds
DataGrid1.DataMember = "Publishers"
' Fill the TreeView with the XmlDataDocument
DisplayXmlNode(xdd, TreeView1.Nodes)
End Sub
' refresh the display
Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
TreeView1.Nodes.Clear()
DisplayXmlNode(xdd, TreeView1.Nodes)
End Sub
' add a node
Private Sub btnAddNode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNode.Click
' Before proceding, you must set EnforceConstraints to False.
ds.EnforceConstraints = False
' Add a new node to the XmlDataElement.
Dim xmlEl As XmlElement = CreateAppendElement(xdd.DocumentElement, "Publishers")
CreateAppendElement(xmlEl, "pub_id", "9997")
CreateAppendElement(xmlEl, "pub_name", "Sci-Fi Publications")
CreateAppendElement(xmlEl, "city", "Boston")
CreateAppendElement(xmlEl, "state", "MA")
CreateAppendElement(xmlEl, "country", "USA")
' Normalizing after XML operations is a good habit.
xdd.Normalize()
' Re-enable constraints.
ds.EnforceConstraints = True
' Display the result in the TreeView.
TreeView1.Nodes.Clear()
DisplayXmlNode(xdd, TreeView1.Nodes)
End Sub
' perform an XPath search
Private Sub bthXPathSearches_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bthXPathSearches.Click
Dim xpath As String = "//Publishers[city = 'Boston']/Titles/title/text()"
Dim xnl As XmlNodeList = xdd.SelectNodes(xpath)
Dim xt As XmlText
Dim msg As String
For Each xt In xnl
msg &= xt.Value & ControlChars.CrLf
Next
MessageBox.Show(msg, "Result")
xpath = "//Publishers[Titles/title/text() = 'Net Etiquette']"
Dim xmlEl As XmlElement = DirectCast(xdd.SelectSingleNode(xpath), XmlElement)
Dim dr As DataRow = xdd.GetRowFromElement(xmlEl)
msg = dr("pub_name").ToString & ControlChars.CrLf
msg &= dr("city").ToString & ControlChars.CrLf
MessageBox.Show(msg, "Result")
End Sub
End Class